package HLUserControl;

import javax.microedition.lcdui.Image;

import HLLib.base.HLGraphics;
import HLLib.base.HLImage;
import HLLib.base.HLScreen;
import HLLib.base.HLString;
import HLLib.control.HLBottom;
import HLLib.control.HLPanel;
import HLLib.control.HLTextPage;
import HLLib.control.HLButton.HLButton_H;
import HLLib.handfere.HLIOManager;
import HLLib.handfere.HLUIRes;

public class HLMessageDownLoad extends HLPanel implements HLButton_H {

    public static final int MESSAGEBOX_EVENT_ITEM_CLICKED = 1;

    public static final int TEXT_COLOR = HLGraphics.RGB(240, 240, 240);

    public static final int BUTTON_SPACE = 5;

    private HLTextPage textBox;

    int buttonWidth = 0;

    public int selectedIndex = 0;

    byte textBoxPos = 15;

    byte borderWidth = 10;

    public int respondKeyLeft = GAME_BUTTON_LEFT | GAME_BUTTON_UP;

    public int respondKeyRight = GAME_BUTTON_RIGHT | GAME_BUTTON_DOWN;

    private Image img[] = HLIOManager.LoadImages1("/messagebox.pak");

    //
    private Image topImg;

    private Image midImg;

    private Image buttomImg;

    public int result;

    private HLString text;

    public boolean useProgress;

    public HLMessageDownLoad(String text) {
        SetMessageBox(new HLString(text), true);
    }

    public HLMessageDownLoad(String text, boolean userProgress) {
        SetMessageBox(new HLString(text), userProgress);
    }

    public void SetMessageBox(HLString text, boolean useProgress) {
        LoadRes();
        this.useProgress = useProgress;
        //
        topImg = img[0];
        midImg = img[1];
        buttomImg = img[2];

        this.text = new HLString(text.string);
        width = (short) topImg.getWidth();
        textBox = new HLTextPage();
        textBox.SettingNormal(-1, 1, 1, 0, TEXT_COLOR, 0, TEXT_STYLE_NORMAL, HCENTER_VCENTER, FONT_SIZE_SMALL);
        textBox.SetSize(width - borderWidth * 2, HLScreen.height);
        textBox.SetText(text);
        textBox.AdjustToAllTextHeight();

        HLPanel panel = new HLPanel();
        panel.height = (short) height;
        panel.dock = DOCK_STYLE_TOP;
        panel.backcolor = 0xffffff;

        height = textBoxPos + textBox.height + 5;
        if (useProgress) {
            height += imgButtom.getHeight() + 5;
        } else {
            height += 5;
        }
        AjustPos(0, 0, HLLib.base.HLScreen.width, HLLib.base.HLScreen.height, HCENTER_VCENTER);

    }

    public int curProgress;

    private int curProgressShow;

    Image imgs[];

    Image imgButtom;

    Image imgBar;

    Image head;

    public void LoadRes() {
        imgs = HLUIRes.uires.GetButtom();
        imgButtom = imgs[1];
        imgBar = imgs[2];
        head = imgs[3];
    }

    public void SetProgress(int progress) {
        curProgress = progress;
    }

    public void SetProgress(int progress, String text) {
        curProgress = progress;
        this.text = new HLString(text);
    }

    public void RenderProgress(HLGraphics g, int screenX, int screenY) {
        curProgressShow += Math.max(1, (curProgress - curProgressShow) >> 2);
        if (curProgressShow > curProgress) {
            curProgressShow = curProgress;
        } else {
            HLString text = new HLString(this.text.string + " " + curProgressShow + "/100");
            textBox = new HLTextPage();
            textBox.SettingNormal(-1, 1, 1, 0, TEXT_COLOR, 0, TEXT_STYLE_NORMAL, HCENTER_VCENTER, FONT_SIZE_SMALL);
            textBox.SetSize(width - borderWidth * 2, HLScreen.height);
            textBox.SetText(text);
            textBox.AdjustToAllTextHeight();
        }
        int startY = screenY + height - 5 - imgButtom.getHeight();
        g.DrawImageAdjustable(new HLImage(imgButtom), 0, startY, HLLib.base.HLScreen.width, HLBottom.GetButtomHeigth(), TRANS_NONE, HCENTER_TOP);
        int startX = 5;
        int showProgress = curProgressShow + startX;
        int max = 100 + startX * 2;
        int x = (HLLib.base.HLScreen.width - imgButtom.getWidth() >> 1);
        int y = startY;
        g.DrawRegion(new HLImage(imgBar), 0, 0, imgBar.getWidth() * showProgress / max, imgBar.getHeight(), x, y);
        g.DrawImageAdjustable(new HLImage(head), x + (imgBar.getWidth() * showProgress / max), startY - 5, 1, HLBottom.GetButtomHeigth(), TRANS_NONE, HCENTER_TOP);
    }

    public void Logic() {
        super.result = 0;
        result = 0;

        super.Logic();

    }

    public void Render(HLGraphics g) {
        int ScreenX = GetScreenX();
        int ScreenY = GetScreenY();
        int midHeight = height - topImg.getHeight() - buttomImg.getHeight();
        g.DrawImage(new HLImage(topImg), ScreenX, ScreenY, TRANS_NONE);
        g.DrawImageFillRect(new HLImage(midImg), ScreenX, ScreenY + topImg.getHeight(), midImg.getWidth(), midHeight, TRANS_NONE);

        g.DrawImage(new HLImage(buttomImg), ScreenX, ScreenY + topImg.getHeight() + midHeight, TRANS_NONE);

        textBox.RenderAdjustable(g, ScreenX, ScreenY + textBoxPos, width, height, HCENTER_TOP);
        if (useProgress) {
            RenderProgress(g, ScreenX, ScreenY);
        }
        super.Render(g);
    }

}
